package cn.koala.key.complaint.controller;

import cn.koala.key.api.ComplaintApi;
import cn.koala.key.common.enums.EnumKeyValue;
import cn.koala.key.common.excel.ExcelUtils;
import cn.koala.key.common.feign.FeignSpringFormEncoder;
import cn.koala.key.common.feign.FeignUtils;
import cn.koala.key.complaint.entity.dto.ComplaintSearchDTO;
import cn.koala.key.complaint.entity.dto.ComplaintSearchModel;
import cn.koala.key.complaint.entity.vo.*;
import cn.koala.key.complaint.service.ComplaintOrderService;
import cn.koala.key.configure.apollo.ApolloKeyManagementAdminConfig;
import cn.seed.common.core.Result;
import cn.seed.common.core.ResultCode;
import cn.seed.common.core.ResultList;
import cn.seed.common.core.ServiceException;
import cn.seed.common.utils.LoggerUtils;
import cn.seed.web.jwt.JwtTokenAuthentication;
import feign.Feign;
import feign.jackson.JacksonDecoder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @author 38752
 */
@RestController
@Api(value = "ComplaintOrderController", tags = "客诉订单管理")
@RequestMapping("/complaintOrder")
public class ComplaintOrderController {

    @Autowired
    private ComplaintOrderService complaintOrderService;

    @ApiOperation(value = "新增客诉单接口")
    @PostMapping("/create")
    public Result<?> complaintOrderRefer(@Valid @RequestBody CustomerComplaintOrderVO complaintOrderVO,HttpServletRequest request) {
        String userCode = JwtTokenAuthentication.getUserCode(request);
        String userName = JwtTokenAuthentication.getJwtUserName(request);
        return complaintOrderService.addComplaintOrder(complaintOrderVO, userCode,  userName);
    }

    @ApiOperation(value = "开始处理投诉单号接口")
    @PostMapping("/processing")
    public Result<?> processComplaintOrder(@RequestParam String orderNo, HttpServletRequest request) {
        String userCode = JwtTokenAuthentication.getUserCode(request);
        String userName = JwtTokenAuthentication.getJwtUserName(request);
        return complaintOrderService.processComplaintOrder(orderNo, userCode, userName);
    }

    @ApiOperation(value = "提交客诉结果")
    @PostMapping("/commit")
    public Result<?> commitCompliantOrder(@RequestBody CustomerComplaintOrderSubmitVO complaintOrderVO, HttpServletRequest request) {
        String userCode = JwtTokenAuthentication.getUserCode(request);
        String userName = JwtTokenAuthentication.getJwtUserName(request);
        return complaintOrderService.commitCompliantOrder(complaintOrderVO, userCode, userName);
    }


    @ApiOperation("区域经理审核客诉结果")
    @PostMapping("/city/approve")
    public Result<?> cityApproveComplaintOrder(@RequestBody CustomerComplaintOrderApproveVO customerComplaintOrderApproveVO, HttpServletRequest request) {
        String userCode = JwtTokenAuthentication.getUserCode(request);
        String userName = JwtTokenAuthentication.getJwtUserName(request);
        return complaintOrderService.cityApproveComplaintOrder(customerComplaintOrderApproveVO, userCode, userName);
    }

    @ApiOperation("总部审核客诉结果")
    @PostMapping("/hq/approve")
    public Result<?> hqApproveComplaintOrder(@RequestBody CustomerComplaintOrderApproveVO customerComplaintOrderApproveVO, HttpServletRequest request) {
        String userCode = JwtTokenAuthentication.getUserCode(request);
        String userName = JwtTokenAuthentication.getJwtUserName(request);
        return complaintOrderService.hqApproveComplaintOrder(customerComplaintOrderApproveVO, userCode, userName);
    }


    @ApiOperation(value = "查看拒绝原因")
    @GetMapping("/refuseReason")
    public Result<ComplaintOperateLogVO> getRefuseReason(@RequestParam String orderNo) {
        return complaintOrderService.getRefuseReason(orderNo);
    }

    @ApiOperation("查询客诉单的操作日志")
    @GetMapping("/operationLog")
    public Result<List<ComplaintOperateLogVO>> listOperateLogByOrderNo(@RequestParam String orderNo) {
        return complaintOrderService.listOperateLogByOrderNo(orderNo);
    }

    @ApiOperation("查询客诉单的操作日志")
    @GetMapping("/getOperateLogById")
    public Result<ComplaintOperateLogVO> getOperateLogById(@RequestParam Long id) {
        return complaintOrderService.getOperateLogById(id);
    }

    @ApiOperation(value = "客诉单详情展示")
    @GetMapping("/show")
    public Result<ComplaintOrderDetailVO> orderDetail(String orderNo) {
        return complaintOrderService.orderDetail(orderNo);
    }

    @ApiOperation(value = "根据条件查询客诉信息")
    @PostMapping("/search")
    public Result<ResultList<ComplaintSearchDTO>> searchByCondition(@RequestBody ComplaintSearchVO complaintSearchVO){
        return complaintOrderService.searchByCondition(complaintSearchVO);
    }

    @ApiOperation("创建/提交/审核时读取投诉单信息")
    @GetMapping("fillComplaintOrder")
    public Result<ComplaintOrderFillResultVO> fillComplaintOrder(@RequestParam(required = false) String orderNo, @RequestParam(required = false) String repairOrderNo) {
        return complaintOrderService.fillComplaintOrder(orderNo, repairOrderNo);
    }

    @ApiOperation(value = "导出客诉单信息")
    @GetMapping("/search/export")
    public void complaintSearchPrint(ComplaintSearchVO complaintSearchVO, HttpServletResponse response){
        try {
            List<ComplaintSearchModel> rModels = complaintOrderService.complaintSearchPrint(complaintSearchVO);
            ExcelUtils.exportExcel(response, ComplaintSearchModel.class, rModels, "客诉单");
        } catch (Exception e) {
            LoggerUtils.error(ComplaintOrderController.class, e.getMessage());
            throw new ServiceException(ResultCode.FAIL, e.getMessage());
        }
    }

    @ApiOperation("客诉单附件上传")
    @PostMapping("/attachment/upload")
    public Result<String> uploadAttachment(@RequestPart MultipartFile file, HttpServletRequest request) {
        String userCode = JwtTokenAuthentication.getUserCode(request);
        return  Feign.builder().decoder(new JacksonDecoder()).encoder(new FeignSpringFormEncoder()).target(ComplaintApi.class, ApolloKeyManagementAdminConfig.getKeyapi()).uploadAttachment(file, userCode);
    }

    @ApiOperation("列出投诉分类问题")
    @GetMapping("sort/list")
    public Result<List<EnumKeyValue>> listComplaintSort(){
        return FeignUtils.getHttpApi(ComplaintApi.class).listComplaintSort();
    }

    @ApiOperation("列出客诉级别")
    @GetMapping("level/list")
    public Result<List<EnumKeyValue>> listComplaintLevel(){
        return FeignUtils.getHttpApi(ComplaintApi.class).listComplaintLevel();
    }
    @ApiOperation("列出客诉单状态")
    @GetMapping("status/list")
    public Result<List<EnumKeyValue>> listComplaintStatus(){
        return FeignUtils.getHttpApi(ComplaintApi.class).listComplaintStatus();
    }
    @ApiOperation("列出客诉单是否消费")
    @GetMapping("consume/spend/list")
    public Result<List<EnumKeyValue>> listComplaintConsumeSpend(){
        return FeignUtils.getHttpApi(ComplaintApi.class).listComplaintConsumeSpend();
    }
    @ApiOperation("列出客诉单来源渠道")
    @GetMapping("source/list")
    public Result<List<EnumKeyValue>> listComplaintSource(){
        return FeignUtils.getHttpApi(ComplaintApi.class).listComplaintSource();
    }
    @ApiOperation("列出客诉单是否有效")
    @GetMapping("valid/list")
    public Result<List<EnumKeyValue>> listComplaintValid(){
        return FeignUtils.getHttpApi(ComplaintApi.class).listComplaintValid();
    }
}
